package 学生管理;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Manage extends JFrame implements ActionListener{
    private JTextField[] tf;
    private JButton btn;
    private JTable table;
    private DefaultTableModel model;
    private List<Student> stus;
    public Manage(){
        setTitle("学生管理");
        setSize(800,400);
        setResizable(false);//设置文本框不可拉动
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(null);
        String[] str={"姓名","年龄","地址",};
        tf=new JTextField[3];
        int y=50;
        for (int i=0;i<str.length;i++,y=y+50){
            JLabel label=new JLabel(str[i]);
            label.setBounds(50,y,60,25);
            add(label);
            tf[i]=new JTextField();
            tf[i].setBounds(110,y,200,25);
           add(tf[i]);
        }
        btn=new JButton("添加");
        btn.setBounds(150,250,60,25);
        btn.addActionListener(this);
        add(btn);
        model=new DefaultTableModel();
        model.setDataVector(new Object[][]{},str);
        table=new JTable(model);
        table.setBounds(350,50,400,300);
        JScrollPane sp=new JScrollPane(table);
        sp.setBounds(350,50,400,300);
        add(sp);
       /* stus=new ArrayList<>();
        stus.add(new Student("wang",20,"wuhua"));
        stus.add(new Student("he",20,"wuhua"));
        stus.add(new Student("gu",20,"wuhua"));*/
        try {
            stus=readFile("d:\\student.dat");
           // writeFile(stus);
            loadData(stus);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //Student s1=new Student("xiaowang",23,"kunming");
        //model.addRow(s1.toArray());

    }
    public void writeFile(List<? extends Student> stus)throws IOException{
        File file=new File("d:\\student.dat");
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
        oos.writeObject(stus);
        oos.close();
    }
    public List<Student> readFile(String path) throws IOException,ClassNotFoundException{
        File file=new File(path);
        List<Student> stus;
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
        stus=(List<Student>) ois.readObject();
        ois.close();
        return stus;

    }
    public void loadData(List<?extends Student> stus){
        int c=model.getRowCount();
        for(int i=0;i<c;i++){
            model.removeRow(0);
        }
        for (Student s:stus){
            model.addRow(s.toArray());
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==btn){
            Student s=new Student();
            s.setName(tf[0].getText());
            s.setAge(Integer.valueOf(tf[1].getText()));
            s.setAddr(tf[2].getText());
           if (stus==null) stus=new ArrayList<>();
            stus.add(s);
            try {
                writeFile(stus);
                loadData(stus);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Manage();

            }
        });
    }

}
